How to CPS Transform a Monad
نویسندگان
چکیده
CPS transformation is an important tool in the compilation of functional programming languages. For strict languages, such as our web programming language “Rinso” or Microsoft’s F#, monadic expressions can help with structuring and composing computations. To apply a CPS transformation in the compilation process of such a language, we integrate explicit monadic abstraction in a call-by-value source language, present a Danvy-Filinski-style CPS transformation for this extension, and verify that the translation preserves simple typing. We establish the simulation properties of this transformation in an untyped setting and relate it to a two stage transformation that implements the monadic abstraction with thunks and introduces continuations in a second step. Furthermore, we give a direct style translation which corresponds to the monadic translation.
منابع مشابه
An Initial Model for the Monadic Calculus
When it comes to compiler design, there has been a dispute as to whether it is useful to transform the source code using the continuation passing style (CPS) transform first. Some have observed that it is easier to optimize CPS transformed code, while others maintain that direct style compilation is better. Sabry and Felleisen resolve the dispute by showing how get the benefits of CPS without t...
متن کاملThe selection monad as a CPS transformation
A computation in the continuation monad returns a final result given a continuation, ie. it is a function with type (X → R) → R. If we instead return the intermediate result at X then our computation is called a selection function. Selection functions appear in diverse areas of mathematics and computer science (especially game theory, proof theory and topology) but the existing literature does ...
متن کاملMonad Translating Inductive and Coinductive Types
We show that the call-by-name monad translation of simply typed lambda calculus extended with sum and product types extends to special and general inductive and coinductive types so that its crucial property of preserving typings and βand commuting reductions is maintained. Specific similar-purpose translations such as CPS translations follow from the general monad translations by specializatio...
متن کاملOn the Overhead of CPS
Continuation-passing style (CPS) is often criticized to be more expensive than the usual direct style of functional programming. By structure, CPS functions indeed are passed one extra argument (the continuation), and each intermediate result indeed occurs in a function call (to the continuation). As higher-order functions, continuations are also more expensive. However, by structure also, CPS ...
متن کاملA functional correspondence between monadic evaluators and abstract machines for languages with computational effects
We extend our correspondence between evaluators and abstract machines from the pure setting of the λ-calculus to the impure setting of the computational λ-calculus. We show how to derive new abstract machines from monadic evaluators for the computational λ-calculus. Starting from (1) a generic evaluator parameterized by a monad and (2) a monad specifying a computational effect, we inline the co...
متن کامل